﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WestWorldRemake;

namespace WestWorldRemake.Characters
{
    class EntityManager
    {
        private Dictionary<string, BaseGameEntity> EntityMap;
        private static EntityManager _Instance;
        private EntityManager() { EntityMap = new Dictionary<string, BaseGameEntity>(); }

        private Dictionary<BaseGameEntity, BaseGameEntity> entitiesToLevel = new Dictionary<BaseGameEntity, BaseGameEntity>();
        private List<BaseGameEntity> entitiesToAdd = new List<BaseGameEntity>();
        private List<BaseGameEntity> entitiesToDrop = new List<BaseGameEntity>();

        private void AddEntities()
        {
            foreach (BaseGameEntity ent in entitiesToAdd)
                EntityMap.Add(ent.name, ent);
            entitiesToAdd = new List<BaseGameEntity>();
        }
        private void DropEntities()
        {
            foreach (BaseGameEntity ent in entitiesToAdd)
                EntityMap.Remove(ent.name);
            entitiesToDrop = new List<BaseGameEntity>();
        }
        private void LevelUpEntities()
        {
            foreach (BaseGameEntity ent in entitiesToLevel.Keys)
                RemoveEntity(ent);
            foreach (BaseGameEntity ent in entitiesToLevel.Values)
                RegisterEntity(ent);
            entitiesToLevel = new Dictionary<BaseGameEntity, BaseGameEntity>();
        }

        public static EntityManager Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new EntityManager();
                return _Instance;
            }
        }
        public void RegisterEntity(BaseGameEntity entity)
        {
            entitiesToAdd.Add(entity);
        }
        public void LevelEntity(BaseGameEntity oldEntity, BaseGameEntity newEntity)
        {
            entitiesToLevel.Add(oldEntity, newEntity);
        }
        public BaseGameEntity GetEntityByName(string name)
        {
            BaseGameEntity ent = null;
            EntityMap.TryGetValue(name, out ent);
            return ent;
        }

        public void RemoveEntity(BaseGameEntity pEntity)
        {
            entitiesToDrop.Add(pEntity);
        }
        public void Update()
        {
            double DevoutSum = 0;

            if (entitiesToAdd.Count > 0)
                AddEntities();

            foreach (BaseGameEntity ent in EntityMap.Values)
            {
                ent.Update();
                if (ent.GetType() == typeof(Creepy))
                    DevoutSum += ((Creepy)ent).Devoutness;
            }

            if (entitiesToLevel.Count > 0)
                LevelUpEntities();
            if (entitiesToDrop.Count > 0)
                DropEntities();

            if (DevoutSum > 9000 && Program.i >= 500 || Program.i == 1999)
                throw new NotImplementedException("Cthulhu not implemented yet");
        }
    }
}
